package com.xzuf.pub;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtils
{

    private static DateUtils singleton = null;
    private static SimpleDateFormat dateFormat = null;
    private static SimpleDateFormat dayFormat = null;
    private static SimpleDateFormat yearFormat = null;
    private static SimpleDateFormat hourFormat = null;

    public DateUtils()
    {
    }

    public static DateUtils getSingleton()
    {
        if(singleton == null)
            init();
        return singleton;
    }

    private static void init()
    {
        if(singleton == null)
        {
            singleton = new DateUtils();
            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            hourFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            dayFormat = new SimpleDateFormat("yyyy-MM-dd");
            yearFormat = new SimpleDateFormat("yyyy");
        }
    }

    public String getCurYear()
    {
        Calendar calendar = Calendar.getInstance();
        String date = yearFormat.format(calendar.getTime());
        return date;
    }

    public String getCurTime()
    {
        Calendar calendar = Calendar.getInstance();
        String date = dateFormat.format(calendar.getTime());
        return date;
    }

    public long getCurTimeInt()
    {
        return System.currentTimeMillis();
    }

    public String getCurDay()
    {
        Calendar calendar = Calendar.getInstance();
        String day = dayFormat.format(calendar.getTime());
        return day;
    }

    public String getCurHour()
    {
        Calendar calendar = Calendar.getInstance();
        String day = hourFormat.format(calendar.getTime());
        return day;
    }

    public boolean checkDate(String pDateObj)
    {
        boolean ret = true;
        if(pDateObj == null || pDateObj.length() < 1)
            ret = false;
        try
        {
            String arr[] = pDateObj.split("-");
            int year = (new Integer(arr[0])).intValue();
            int month = (new Integer(arr[1])).intValue();
            int day = (new Integer(arr[2])).intValue();
            Calendar cal = Calendar.getInstance();
            cal.setLenient(false);
            cal.set(year, month - 1, day);
            cal.getTime();
        }
        catch(Exception e)
        {
            ret = false;
        }
        return ret;
    }

}
